<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\v1\controller;

use api\v1\model\Advertise;
use api\v1\model\Article;
use api\v1\model\Category;
use api\v1\model\Lottery;
use api\v1\model\Option;
use api\v1\model\Order;
use api\v1\model\Product;
use api\v1\model\User;
use cmf\controller\RestBaseController;
use cmf\controller\RestUserBaseController;
use think\facade\Db;

/**
 * 首页接口
 * Class IndexController
 * @package api\v1\controller
 */
class ProductController extends RestUserBaseController
{

    public function index() {
        $data = [];
        $data['list'] = Category::where("type",1)->order("sort desc")->select();
        $this->success("ok",$data);
    }
    public function product() {
        $id = $this->request->param('id');
        $res = Product::find($id);
        $this->success("ok",$res);
    }
    public function addorder() {
        Db::startTrans();
        try {
            $product = Product::where("id", $this->request->post("id"))->lock(true)->find();
            $user = User::where("id",$this->userId)->lock(true)->find();
            $amount = floatval($this->request->param('amount'));
            if ($amount < $product->qtje) {
                $msg = "最低投资" . $product->qtje;
                throw new \Exception($msg);
            }
            $max = floatval($product->xmgm)*10000 - floatval($product->xmjd);
            if ($amount > $max) {
                $amount = $max;
            }
            if($amount>$user->balance) {
                throw new \Exception("余额不足");
            }
            $remark = $product->id.":".$product->title;
            User::money_change($user->id,-$amount,3,$remark);
            if($product->hongbao) {
                User::money_change($user->id,$product->hongbao,4,$remark);
            }
            $jifen = intval($amount);
            if($product->jifen) {
                $jifen = $jifen * 2;
            }
            User::score_change($user->id,$jifen,1,$remark);
            $order = new Order();
            $order->user_id = $user->id;
            $order->product_id = $product->id;
            $order->amount = $amount;
            $order->useritem_time = time();
            //$order->rate = $product->jyrsy * (1+$product->fenhong/100);
            $order->rate = $product->jyrsy;
            $order->fenhong = $product->fenhong;
            $order->shichang = $product->shijian;
            $order->ip = $this->request->ip();
            $order->save();
            $product->xmjd = $product->xmjd + $amount;
            $product->save();
            $site_info = Option::where("option_name", "site_info")->find()->option_value;
            $this->fenxiao($user->pid,$amount,"用户：".$user->user_nickname,1,3,$site_info);
            Db::commit();
        }
        catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success("操作成功！");
    }
    private function fenxiao($pid,$amount,$remark,$i=1,$total=3,$siteInfo=[]) {
        if($i <= $total) {
            $parent = User::find($pid);
            if ($parent) {
                if(isset($siteInfo["fan_".$i])) {
                    $rate = floatval($siteInfo['fan_'.$i]);
                    $money = intval($amount * $rate)/100;
                    User::money_change($parent->id,$money,6,$remark);
                }
                $this->fenxiao($parent->pid,$amount,$remark,++$i,$total,$siteInfo);
            }
        }

    }
}
